package com.atguigu.ssyx.acl.controller;


import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController

@Api(tags = "用户接口")
@RequestMapping("/admin/acl/user")
//@CrossOrigin
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private RoleService roleService;


    //为用户进行分配角色

    //参数有用户id 和 多个角色id（数组）
    @ApiOperation("为用户进行角色分配")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long adminId,
                           @RequestParam Long[] roleId){
        roleService.saveAdminRole(adminId,roleId);
        return Result.ok(null);

    }

    //获取所有角色，和根据用户id查询用户分配角色列表
    @ApiOperation(value = "获取所有角色")
    @GetMapping("/toAssign/{adminId}")
    public  Result toAssign(@PathVariable Long adminId){
            //返回map集合包含两部分数据： 所有角色 和 为用户分配角色列表
            Map<String,Object> map = roleService.getRoleByAdminId(adminId);
            return Result.ok(map);
    }

    //1 用户列表
    @ApiOperation(value = "角色列表（条件分页查询）")
    @GetMapping("{current}/{limit}")
    public Result list(@PathVariable Long current,
                           @PathVariable Long limit,
                       AdminQueryVo adminQueryVo){

        //1 创建page对象， 传递当前页和每页记录数
        //current:当前页
        //limit :每页显示记录数
        Page<Admin> pageParam = new Page<>(current,limit);


        //2 调用Service方法实现条件分页查询，返回分页对象
        IPage<Admin> pageModel = adminService.selectPageUser(pageParam,adminQueryVo);

        return Result.ok(pageModel);

    }

    //2 id查询用户
    @ApiOperation(value = "根据id查询")

    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id){
        Admin admin = adminService.getById(id);
        return Result.ok(admin);

    }


    //3 添加用户

    @ApiOperation(value = "添加用户")
    @PostMapping("save")
    public  Result save(@RequestBody Admin admin){
        //获取用户密码
        String password= admin.getPassword();
        //加密用户密码
        String passwordMD5= MD5.encrypt(password);
        //设置加密后的密码
        admin.setPassword(passwordMD5);

         adminService.save(admin);
        return Result.ok(null);
    }

    //4 修改删除
    @ApiOperation(value = "修改用户")
    @PutMapping("update")
    public Result update(@RequestBody Admin admin){
        adminService.updateById(admin);
        return Result.ok(null);
    }

    //5 id删除
    @ApiOperation(value = "根据id删除用户")
    @DeleteMapping("remove/{id}")
    public  Result remove(@PathVariable Long id){
        adminService.removeById(id);
        return  Result.ok(null);
    }

    //6 批量删除
    @ApiOperation(value = "批量删除用户")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        adminService.removeByIds(idList);
        return Result.ok(null);
    }
}
